var hx = require("hbuilderx");

/**
 * 获取当前HBuilderX在硬盘中的路径
 */
function getAppRoot() {
	return hx.env.appRoot
}

/**
 * 获取workspaceFolder里的某个字段数据
 * @param {Object} key workspaceFolder里的key，比如uri
 */
async function getWorkspaceFolderData(key) {
	// 获取当前的项目管理器，比如uhaozu-mobile
	let activeEditor = await hx.window.getActiveTextEditor();
	return activeEditor.document.workspaceFolder[key]
}

/**
 * 获取manifest.json的某个数据
 */
async function getManifestData(key) {
	const activeFolderPath = (await getWorkspaceFolderData('uri')).path.replace(/^\//, '')
	const manifestPath = `${activeFolderPath}/manifest.json`
	if(Array.isArray(key)) {
		let res = []
		for (let index = 0; index < key.length; index++) {
			const value = key[index];
			res.push((await hx.util.readJSONValue(manifestPath, value)).data)
		}
		return res
	} else {
		return (await hx.util.readJSONValue(manifestPath, key)).data
	}
}



module.exports = {
	getAppRoot,
	getWorkspaceFolderData,
	getManifestData
}
